package boot

import (
	"fmt"
	"io/ioutil"
	"os"

	. "gitee.com/edgebox/metadata/internal/model"
	"gitee.com/edgebox/pkg/db"
	"gopkg.in/yaml.v2"
)

type database struct {
	Name string `yaml:"name"`
	Dsn  string `yaml:"dsn"`
}

type mqtt_server struct {
	Url    string `yaml:"url"`
	User   string `yaml:"user"`
	Passwd string `yaml:"passwd"`
}
type config struct {
	Device_type *string      `yaml:"device_type"`
	Device_name *string      `yaml:"device_name"`
	Database    *database    `yaml:"database"`
	Mqtt        *mqtt_server `yaml:"mqtt"`
}

func init() {
	args := os.Args
	path := "./config.yaml"
	var driver string = "sqlite"
	var dsn string = "edgebox.db"
	if len(args) > 1 {
		path = args[1]
	}
	var conf config
	if yamlFile, err := ioutil.ReadFile(path); err != nil {
		conf.Database = &database{}
		conf.Database.Name = driver
		conf.Database.Dsn = dsn
		fmt.Println(err)
	} else {
		err = yaml.Unmarshal(yamlFile, &conf)
		if err == nil {
			if conf.Database != nil {
				driver = conf.Database.Name
				dsn = conf.Database.Dsn
			}
		} else {
			fmt.Println(err)
		}
	}
	if savedata, err := yaml.Marshal(conf); err == nil {
		ioutil.WriteFile(path, savedata, os.ModePerm)
	}

	fmt.Println("sqlite:", "dbpath")
	fmt.Println("mysql:", "user:psw@tcp(localhost:3306)/dbname?charset=utf8&parseTime=True&loc=Local")
	fmt.Println("postgres:", "host=localhost port=5432 user=postgres dbname=dbname password='psw' sslmode=disable")
	//dsn :="host=127.0.0.1 port=5432 user=postgres dbname=gorm password='1314520' sslmode=disable"
	//dsn := "guirf:1314520@tcp(localhost:3306)/hlhz?charset=utf8&parseTime=True&loc=Local"
	//dsn := "edgebox.db"
	if err := db.Register(driver, dsn); err != nil {
		fmt.Println("database register error:", err)
	} else {
		ModelInit()
		if conf.Device_name != nil {
			kp := KeyPair{
				Id:    "device_name",
				Name:  "设备名称",
				Value: *conf.Device_name,
			}
			kp.Save()
		} else {
			name, _ := os.Hostname()
			kp := KeyPair{
				Id:    "device_name",
				Name:  "设备名称",
				Value: name,
			}
			kp.Save()
		}
		if conf.Device_type != nil {
			kp := KeyPair{
				Id:    "device_type",
				Name:  "设备类型",
				Value: *conf.Device_type,
			}
			kp.Save()
		} else {
			kp := KeyPair{
				Id:    "device_type",
				Name:  "设备类型",
				Value: "edgebox",
			}
			kp.Save()
		}
		if conf.Mqtt != nil {
			kp_1 := KeyPair{
				Id:    "mqtt_server",
				Name:  "mqtt_服务器地址",
				Value: conf.Mqtt.Url,
			}
			kp_2 := KeyPair{
				Id:    "mqtt_user",
				Name:  "mqtt用户名",
				Value: conf.Mqtt.User,
			}
			kp_3 := KeyPair{
				Id:    "mqtt_psw",
				Name:  "mqtt密码",
				Value: conf.Mqtt.Passwd,
			}
			kp_1.Save()
			kp_2.Save()
			kp_3.Save()
		} else {
			kp_1 := KeyPair{
				Id:    "mqtt_server",
				Name:  "mqtt_服务器地址",
				Value: "127.0.0.1:1883",
			}
			kp_2 := KeyPair{
				Id:    "mqtt_user",
				Name:  "mqtt用户名",
				Value: "admin",
			}
			kp_3 := KeyPair{
				Id:    "mqtt_psw",
				Name:  "mqtt密码",
				Value: "passwd",
			}
			kp_1.Save()
			kp_2.Save()
			kp_3.Save()
		}
		InitApp()
	}
}
